home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / program / commio0b.zip / INIUNIT.PAS < prev    next >
Pascal/Delphi Source File  |  1996-05-02  |  9KB  |  270 lines

  1. {$O+,F+}        {Overlay it to save memory... since this is basically only
  2.                  used once in a program.}
  3. unit INIunit;
  4. {
  5.           This unit is a companion to the COMMIO communications unit.
  6.                 Written by Jason Morriss a.k.a. Lief O'Pardy
  7.  
  8.                   Copyright (C) 1995,1996 by Jason Morriss
  9. }
  10. interface
  11.  
  12. uses dos,doorio;  {doorio for killextrablanks()}
  13.  
  14. type
  15.   String30 = string[30];
  16.   TiniVal = record (* INI values (when reading an INI file) *)
  17.     case n:byte of
  18.       1 : (st:string[222]); {should not be larger then 222}
  19.       2 : (li:longint);
  20.       3 : (w:word);
  21.       4 : (i:integer);
  22.       5 : (b:byte);
  23.       6 : (si:shortint);
  24.       7 : (bool:boolean);
  25.   end;
  26.  
  27. const
  28.   IniString   = 1;
  29.   IniLongint  = 2;
  30.   IniWord     = 3;
  31.   IniInteger  = 4;
  32.   IniByte     = 5;
  33.   IniShortint = 6;
  34.   IniBoolean  = 7;
  35.  
  36. const
  37.   ErrOk        = 0;
  38.   ErrNotFound  = 1;
  39.   ErrDiskError = 2;
  40.   ErrNoMemory  = 3;
  41.  
  42.   Ok           = 0;
  43.   KeyNotFound  = 4;
  44.  
  45. Function OpenIni(filename:pathstr):byte;
  46. {^ This prepares the INI file to be read from, and sets up a buffer for
  47.    the reading of the keynames.  Returns one of the ERR codes shown above.
  48.    (This tries to allocate at max, a 64k buffer but will use less if that
  49.    much is not available).
  50.    NOTE: until i create a "SetIniVal()" proc. this function will actually
  51.          CLOSE the ini file, but will leave the buffer alone.  The CloseINI()
  52.          procedure will only free the buffer, so you still must call that. }
  53. Function GetIniVal(keyname:string30; valtype:byte; var val:TIniVal):byte;
  54. {^ Get a value from the INI file.  This is called as many times as you need
  55.    after you have OPENED the ini file with the above function.  OpenINI()
  56.    loads all the keynames and thier values into MEMORY, so this function
  57.    does not need to access the disk.
  58.    Keyname : Search for this keyname string.
  59.    valtype : What type of data are you looking for?
  60.              1=string; 2=longint; 3=word; 4=integer; 5=byte; 6=shortint;
  61.              7=boolean;
  62.    val     : If Keyname was found then this holds the data, else its 0.  If
  63.              the data that was found is invalid for the valtype you gave, a
  64.              0 is returned in val. (ie: you want an WORD and the data
  65.              found was "1024k" (w/o qoutes); that would return a 0 in val) }
  66. Procedure CloseIni;
  67. {^ Close the INI file.  This also Kills the buffer used by the keynames.
  68.    (see note in description of OpenINI() function above) }
  69. {
  70.   INI file format & manageability:
  71.   ────────────────────────────────
  72.   NOTES:
  73.    ■ The maximum number of Keynames allowed in a single INI file is 255
  74.    ■ Only the first 255 chars on a line are read, anymore are ignored
  75.   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  76.   Keynames:   Keynames must be ONE word and can consist of any character,
  77.               except the following: [';',' ',#0..#31].  Keynames can be any
  78.               length from 1 to 30 chars, if a keyname is any longer, the
  79.               remaining chars will be ignored, so the following 2 keynames
  80.               would have the same keyname according to these routines, and
  81.               probably cause trouble while trying to read the ini file:
  82.                 This_is_my_30_character_keyname_#one=1
  83.                 This_is_my_30_character_keyname_#two=2
  84.               All Keynames are automatically uppercased when read, so its
  85.               not case sensitive.
  86.   Delimiter:  The delimiter is what seperates the keyname from the value. ie:
  87.               mykeyname=myvalue ;The "=" is the delimiter...
  88.               as of now, only an "=" (equal-sign) may be used... But future
  89.               versions will allow different delimiters and that tells the
  90.               pharser how to read the "value".
  91.   Value:      Values can have up to 222 chars on its line (after the
  92.               delimiter).
  93.               The Value comes right after the delimiter.  And can be any
  94.               one of the following types:
  95.               a String, longint, word, integer, byte, shortint, boolean.
  96.  
  97.               BOOLEAN values can be represented in a few different ways.
  98.               values for TRUE : 'yes','on','1'
  99.               values for FALSE: 'no','off','0'
  100.               Boolean values do not use double quotes like strings below.
  101.               (boolean values should NOT have quotes at all)
  102.  
  103.               STRING values should be enclosed in `"` (double quotes), the
  104.               string itself can also have double quotes in it, w/o doing
  105.               anything special. (ie: StrValue="This is my "Str" with its
  106.               own double quotes '"'").  This is so that the strings in the
  107.               ini file can have leading & trailing spaces, w/o the quotes
  108.               those extra spaces would be deleted,  uhmmm, that probably just
  109.               confused a bunch of people! don't worry 'bout it.
  110.  
  111.   (The keyname+delimiter+value do not have to be directly next to each other,
  112.    ie: [mykey =   "this value"] is the same as: [mykey="this value"])
  113.  
  114.   Comments:   A Comment must be on its own line and start with a ";"
  115.               (semi-colon).
  116.               heres an example:
  117.                 ..
  118.                 ;Current HI Score (this line is completely ignored)
  119.                 hiscore=123456789
  120.                 ..
  121. }
  122.  
  123. type
  124.   Tinikey = record
  125.     keyname : string30;
  126.     value   : string[222];
  127.     changed : boolean;
  128.   end;
  129.   TKeyArray = array[1..1] of Tinikey;
  130.  
  131. var
  132.   INIval : ^TKeyArray;
  133.   MaxKeynames : integer;
  134.   NumKeys     : integer;
  135.  
  136. implementation
  137.  
  138. const
  139.   INIisOpen : boolean = false;
  140. var
  141.   inivalsize : word;
  142.   INIname : string[79];
  143.   INIfile : text;
  144.  
  145. {───────────────────────────────────────────────────────────────────────────}
  146. Function OpenIni;
  147. var
  148.   ws : string;           {work string}
  149.   tmpkey : Tinikey;      {temp keyname}
  150.   ch : char;             {work char}
  151.   i : integer;
  152. begin
  153.   If INIisOpen then exit;
  154.   if filename='' then begin
  155.     INIisOpen:=false;
  156.     OpenIni:=ErrNotFound;
  157.     exit;
  158.   end;
  159. {$I-}
  160.   assign(inifile,filename);
  161.   reset(inifile);
  162. {$I+}
  163.   i:=IOresult;
  164.   if i>2 then i:=2;
  165.   INIisOpen:=(i=0);
  166.   OpenIni:=i;
  167.   if INIisOpen then begin
  168.     if MaxAvail>=65520
  169.       then MaxKeynames:=65520 div sizeof(Tinikey)
  170.       else MaxKeynames:=MaxAvail div sizeof(Tinikey);
  171.     inivalsize:=MaxKeynames * sizeof(Tinikey);
  172.     getmem(inival,inivalsize);
  173.     fillchar(inival^,inivalsize,0);
  174.     NumKeys:=0;
  175.     INIname:=filename;
  176.   end else exit;
  177.  
  178.   (* read in the INI file into the Keyname array *)
  179.   While (not eof(inifile))and(NumKeys<MaxKeynames) do begin
  180.     read(inifile,ws);
  181.     KillExtraBlanks(ws);
  182.     if (ws[1]=';')or(ws='') then begin      {if line is a comment or blank,}
  183.       readln(inifile);                      { ignore it}
  184.     end else begin                          {else proccess it}
  185.       i:=1;
  186.       fillchar(tmpkey,sizeof(tmpkey),0);
  187.       {v- read until a delimiter or blank is found}
  188.       while not (ws[1] in ['=',' '])and(i<31) do begin
  189.         tmpkey.keyname:=tmpkey.keyname+upchar(ws[1]);
  190.         inc(i);
  191.         delete(ws,1,1);
  192.       end;
  193.       {v- incase keyname was more then 1 word, or delimiter was spaced away}
  194.       while not (ws[1] in ['='])and(ws<>'') do delete(ws,1,1);
  195.  
  196.       {v- read in value}
  197.       if ws<>'' then begin
  198.         ch:=ws[1];
  199.         delete(ws,1,1);
  200.         while(ws[1]=' ') do delete(ws,1,1);
  201.         i:=1;
  202.         {v- value is read differently depending on delimiter}
  203.         case ch of
  204.           '=' : begin
  205. {            while (ws<>'')and(i<223) do begin{}
  206.               tmpkey.value:=upcasestr(ws);
  207.               ws:='';
  208. {              tmpkey.value:=tmpkey.value+ws[1];
  209.               inc(i);
  210.               delete(ws,1,1);
  211.             end;{}
  212.             inc(NumKeys);
  213.             inival^[NumKeys]:=tmpkey;
  214.           end;
  215.         end; {of case}
  216.       end;
  217.       readln(inifile);
  218.     end;
  219.   end;
  220. {$I-}
  221.   close(inifile);
  222. {$I+}
  223. end;
  224. {───────────────────────────────────────────────────────────────────────────}
  225. Function GetI